package com.yunfei.springbootgoods.controller;

import com.yunfei.springbootgoods.dto.Page;
import com.yunfei.springbootgoods.dto.Result;
import com.yunfei.springbootgoods.entity.Goods;
import com.yunfei.springbootgoods.exception.MyException;
import com.yunfei.springbootgoods.exceptionEnums.ExceptionEnum;
import com.yunfei.springbootgoods.service.GoodsService;
import com.yunfei.springbootgoods.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ShoppingCarService shoppingCarService;

    /**
     * 获取商品分页列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/getGoodsPage")
    public Result getGoodsPage(Integer pageNo,Integer pageSize){

        try {
            Page goodsPage = goodsService.getGoodsPage(pageNo,pageSize);
            Result result = new Result();
            result.setData(goodsPage);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.ERROR_GOODS_LIST);
        }

    }

    /**
     * 添加商品
     * @param goods
     * @return
     */
    @PostMapping(value = "/addGoods")
    public Result addGoods(Goods goods){
        System.out.println(goods);
        try {
            goodsService.addGoods(goods);
            Result result = new Result();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.ERROR_GOODS_ADD);
        }
    }

    /**
     * 删除商品
     * @param goodsId
     * @return
     */
    @GetMapping(value = "/deleteGoods")
    public Result deleteGoods(Integer goodsId){
        try {

            goodsService.deleteGoods(goodsId);
            Result result = new Result();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.ERROR_GOODS_DELETE);
        }
    }

    /**
     * 更新商品
     * @param goods
     * @return
     */
    @PostMapping(value = "/updateGoods")
    public Result updateGoods(Goods goods){
        try {
            goodsService.updateGoods(goods);
            Result result = new Result();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.ERROR_GOODS_UPDATE);
        }
    }

    /**
     * 添加商品进入购物车
     * @param goodsId
     * @return
     */
    @GetMapping(value = "/addShoppingCar")
    public Result addShoppingCar(Integer goodsId){

        try {
            goodsService.updateStock(goodsId);

            shoppingCarService.addShoppingCar(goodsId);

            Result result = new Result();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ExceptionEnum.ERROR_CAR_ADD);
        }
    }


}
